package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import code.KeyStoreManager;
import code.Language;
import code.Settings;

public class KeyCreationAsymmetric extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	
	private JTextField jTextField1 = null;
	private JTextField jTextField2 = null;
	private JPasswordField jPasswordField = null;
	JPanel prevclass;
	String prevclassvalue;
	
	private HashMap labels;
	ATOCOmain atocoframe;
	private KeyStoreManager ksm;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	Color generate = new Color(59, 89, 152);
	private Settings s;
	
	/**
	 * This is the default constructor
	 */
	public KeyCreationAsymmetric(ATOCOmain atocoframe, JPanel prevclass, String prevclassvalue) {
		super();
		
		Settings s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "KeyCreationAsymmetric", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("personalKeyStoreFile"), 
				(String)Settings.global.get("personalKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("personalKeyStoreFile"), 
    				(String)Settings.global.get("personalKeyStorePassword"));
        }
		
		this.prevclass=prevclass;
		this.prevclassvalue=prevclassvalue;
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel8 = new JLabel((String) labels.get("Create"), SwingConstants.CENTER);
		jLabel8.setBounds(new Rectangle(192, 242, 129, 41));
		jLabel8.setBackground(generate);
		jLabel8.setForeground(Color.WHITE);
		jLabel8.setOpaque(true);
		jLabel8.setFont(new Font("Calibri", Font.BOLD, 14));
		jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				createKey(
						jLabel5.getText(), Integer.parseInt(jTextField1.getText()),
						jTextField2.getText(), jPasswordField.getText().toString()
						);
			}
		});
		
		
		jLabel7 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel7.setBounds(new Rectangle(330, 195, 25, 25));
		jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel6.setBounds(new Rectangle(330, 155, 25, 25));
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(40, 195, 120, 25));
		jLabel4.setText((String) labels.get("KeyPassword"));
		jLabel3 = new JLabel();
		jLabel3.setBounds(new Rectangle(40, 155, 120, 25));
		jLabel3.setText((String) labels.get("KeyAlias"));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(40, 115, 120, 25));
		jLabel2.setText((String) labels.get("KeySize"));
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(40, 75, 120, 25));
		jLabel1.setText((String) labels.get("KeyAlgorithm"));
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(38, 22, 345, 29));
		jLabel.setText((String) labels.get("Header"));
		this.setSize(741, 399);
		this.setLayout(null);
		this.setBackground(Color.WHITE);
		this.add(jLabel, null);
		this.add(jLabel1, null);
		this.add(jLabel2, null);
		this.add(jLabel3, null);
		this.add(jLabel4, null);
		this.add(getJLabel5(), null);
		this.add(getJTextField1(), null);
		this.add(getJTextField2(), null);
		this.add(getJPasswordField(),null);
		this.add(jLabel6, null);
		this.add(jLabel7, null);
		this.add(jLabel8, null);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JLabel getJLabel5() {
		if (jLabel5== null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(200, 75, 120, 25));
			jLabel5.setText("RSA");
		}
		return jLabel5;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(200, 115, 120, 25));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(200, 155, 120, 25));
		}
		return jTextField2;
	}
	
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(200, 195, 120, 25));
		}
		return jPasswordField;
	}

	private void createKey(String KeyAlg, Integer keySize, String keyAlias, String keyPass) {

			ksm.saveKeyPair(keyAlias, ksm.createKeyPair(KeyAlg, keySize), keyPass);
		
			ksm.saveKeyStore();
		
			setprevclasskey(keyAlias, keyPass);

	}
	
	private void setprevclasskey(String keyAlias, String keyPassword){
		if(prevclassvalue.equalsIgnoreCase("Sign")){
			SignDigitalSignature sign=(SignDigitalSignature)prevclass;
//			sign.setKeys((PublicKey)ksm.getPublicKey(keyAlias),
//					(PrivateKey)ksm.getPrivateKey(keyAlias, keyPassword));
			sign.setKeyAndCert(ksm.getCertificate(keyAlias),
					(PrivateKey)ksm.getPrivateKey(keyAlias, keyPassword));
			atocoframe.contentPane(sign);
		} else if(prevclassvalue.equalsIgnoreCase("KeyCreation")){
			KeyCreation keyc = (KeyCreation)prevclass;
			atocoframe.contentPane(keyc);
		} else if(prevclassvalue.equalsIgnoreCase("AsymmetricRSAEnc")){
			AsymmetricRSAEncryption sign=(AsymmetricRSAEncryption)prevclass;
			sign.setCert(ksm.getCertificate(keyAlias));
			atocoframe.contentPane(sign);
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
